package online.westonline.com.westonline.ui;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.easy.west.westclient.Constants;
import com.easy.west.westclient.api.constacts.bean.PersonBean;
import com.easy.west.westclient.api.constacts.event.FindPersonByUid;
import com.easy.west.westclient.api.login.bean.User;
import com.easy.west.westclient.api.login.event.FindUserEvent;
import com.easy.west.westclient.api.meeting.bean.OrderedMeetingBean;
import com.easy.west.westclient.api.meeting.event.FindOrderdMeetingEvent;
import com.hhl.library.FlowTagLayout;

import net.wujingchao.android.view.SimpleTagImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.MyApplication;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.notify.NotifyConstactsAdpter;
import online.westonline.com.westonline.adapter.tag.TagAdapter;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.entry.PushDataBean;
import online.westonline.com.westonline.entry.Tag;
import online.westonline.com.westonline.utils.ContactsUtils;
import online.westonline.com.westonline.utils.ToastUtils;
import online.westonline.com.westonline.utils.UiUtlis;

/***
 * 用于展示会议详情的
 */
public class MeetRoomStateActivity extends BaseActivity {
    @BindView(R.id.tv_topbar_title)
    TextView tvTopbarTitle;
    @BindView(R.id.tv_topbar_title_center)
    TextView tvTopbarTitleCenter;
    @BindView(R.id.img_toolbar_menu)
    ImageView imgToolbarMenu;
    @BindView(R.id.tv_topbar_menu)
    TextView tvTopbarMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.img_meeting_state_bg)
    SimpleTagImageView imgMeetingStateBg;
    @BindView(R.id.tv_order_meet_name)
    TextView tvOrderMeetName;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_order_type)
    TextView tvOrderType;
    @BindView(R.id.li_meeting_desc)
    LinearLayout liMeetingDesc;
    @BindView(R.id.tv_meeting_room_state)
    TextView tvMeetingRoomState;
    @BindView(R.id.tv_task_desc)
    TextView tvTaskDesc;
    @BindView(R.id.recyclerView_task_cosntacts)
    RecyclerView recyclerViewTaskCosntacts;
    @BindView(R.id.li_shenqing)
    LinearLayout liShenqing;
    @BindView(R.id.recyclerView_shengpiren_cosntacts)
    RecyclerView recyclerViewShengpirenCosntacts;
    @BindView(R.id.li_shengpi)
    LinearLayout liShengpi;
    @BindView(R.id.tv_meeting_department)
    TextView tvMeetingDepartment;
    @BindView(R.id.tv_meeting_date)
    TextView tvMeetingDate;
    @BindView(R.id.tv_meeting_starttime)
    TextView tvMeetingStarttime;
    @BindView(R.id.tv_meeting_personcount)
    TextView tvMeetingPersoncount;
    @BindView(R.id.tv_meeting_leader)
    TextView tvMeetingLeader;
    @BindView(R.id.tv_meeting_constacts)
    TextView tvMeetingConstacts;
    @BindView(R.id.tv_meeting_constacts_phone)
    TextView tvMeetingConstactsPhone;
    @BindView(R.id.tv_call)
    TextView tvCall;
    @BindView(R.id.li_department)
    LinearLayout liDepartment;
    @BindView(R.id.meeting_layout)
    LinearLayout meetingLayout;
    @BindView(R.id.tv_meeting_sit)
    TextView tvMeetingSit;
    @BindView(R.id.li_sitdown_card)
    LinearLayout liSitdownCard;
    @BindView(R.id.tv_meeting_flag)
    TextView tvMeetingFlag;
    @BindView(R.id.li_meeting_flag)
    LinearLayout liMeetingFlag;
    @BindView(R.id.tag_equa_layout)
    FlowTagLayout tagEquaLayout;
    @BindView(R.id.li_equea_layout)
    LinearLayout liEqueaLayout;
    @BindView(R.id.tv_to_department)
    TextView tvToDepartment;
    private PushDataBean dataBean;
    private String mode;
    private String TASK_MODE = Constants.NOTIFIY_TYPE_TASK;
    private String MEETING_MODE = Constants.NOTIFIY_TYPE_MEETING;
    private OrderedMeetingBean meetingBean;
    private NotifyConstactsAdpter constactsAdpter;
    private List<PersonBean> userList = new ArrayList<>();
    private TagAdapter<Tag> tagAdapter;

    @Override
    protected void initview() {

        constactsAdpter = new NotifyConstactsAdpter();
        recyclerViewTaskCosntacts.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
        recyclerViewTaskCosntacts.setAdapter(constactsAdpter);
        initsession();

    }

    /**
     * 事件
     */
    private void initsession() {
        //电话拨打
        tvCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContactsUtils.diallPhone(meetingBean.getData().getPhonenum(),MeetRoomStateActivity.this);
            }
        });
        //前往部门
        tvToDepartment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("id",meetingBean.getData().getDepartmentDTO().getDid());
                startActivityWithBundle(DepInfoActivity.class,false,bundle);
            }
        });
    }

    //1,投影仪 2,麦克风 3,会标 4,电视 5,无线话筒 6,音响 7,座牌 8,告示牌 9,茶水 10,矿泉水
    //初始化tag
    private void inittag() {
        List<Tag> equa_list = new ArrayList<>();
        tagAdapter = new TagAdapter<>(this);

        if (meetingBean.getData().getEquipment() != null) {
            liEqueaLayout.setVisibility(View.VISIBLE);
            String equa = (String) meetingBean.getData().getEquipment();
            String[] items = equa.split(",");
            for (String item : items) {
                int int_item = Integer.parseInt(item);
                switch (int_item) {
                    case 1:
                        equa_list.add(new Tag(1, "投影仪"));
                        break;
                    case 2:
                        equa_list.add(new Tag(2, "麦克风"));
                        break;
                    case 3:
                        equa_list.add(new Tag(3, "会标"));
                        break;
                    case 4:
                        equa_list.add(new Tag(4, "电视"));
                        break;
                    case 5:
                        equa_list.add(new Tag(5, "无线话筒"));
                        break;
                    case 6:
                        equa_list.add(new Tag(6, "音响"));
                        break;
                    case 7:
                        equa_list.add(new Tag(7, "座牌"));
                        break;
                    case 8:
                        equa_list.add(new Tag(8, "告示牌"));
                        break;
                    case 9:
                        equa_list.add(new Tag(9, "茶水"));
                        break;
                    case 10:
                        equa_list.add(new Tag(10, "矿泉水"));
                        break;
                }

            }
            tagAdapter.setSelectable(false);
            tagAdapter.onlyAddAll(equa_list);
            tagEquaLayout.setAdapter(tagAdapter);
            tagAdapter.notifyDataSetChanged();
            tagEquaLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_NONE);
        } else {
            liEqueaLayout.setVisibility(View.GONE);
        }

    }

    @Override
    protected void initdata() {
        showProgessDialog("加载中");
        dataBean = (PushDataBean) getIntent().getSerializableExtra("data");
        if (dataBean != null) {
            if (dataBean.getMeeting() != null) {
                //会议模式
                mode = MEETING_MODE;
                //请求预定会议室列表
                int id = Integer.parseInt(dataBean.getMeeting());
                MyApplication.getClient().findorderedMeet(id);

            }
            if (dataBean.getTask() != null) {

            }
            if (dataBean.getComment() != null) {

            }
        }
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_meet_room_state;
    }


    private void initui() {
        //获取申请人
        MyApplication.getClient().findConstactsbyuid(meetingBean.getData().getUid());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        dissmissDialog();
        //设置toolbar
        //组装数据
        if (mode.equals(MEETING_MODE)) {
            toolbar.setTitle("会议室申请提醒");
            meetingLayout.setVisibility(View.VISIBLE);
            tvTaskDesc.setText("申请人");
            switch (meetingBean.getData().getState()) {
                case 0:
                    tvMeetingRoomState.setText("审核中");
                    break;
                case 1:
                    tvMeetingRoomState.setText("已同意申请");
                    break;
                case 2:
                    tvMeetingRoomState.setText("申请已撤销");
                    tvMeetingRoomState.setTextColor(UiUtlis.getColor(R.color.colorMenuBtn));
                    break;
                case -1:
                    tvMeetingRoomState.setText("申请被拒绝");
                    tvMeetingRoomState.setTextColor(UiUtlis.getColor(R.color.TopicItemNodeTextColor));
                    break;

            }
            tvOrderMeetName.setText(meetingBean.getData().getMeetingRoomDTO().getName());
            Glide.with(this).load(meetingBean.getData().getMeetingRoomDTO().getImg()).into(imgMeetingStateBg);
            if (meetingBean.getData().getMeetingRoomDTO().isVip()) {
                imgMeetingStateBg.setTagEnable(true);
            } else {
                imgMeetingStateBg.setTagEnable(false);
            }
            switch (meetingBean.getData().getMeetingRoomDTO().getSize()) {
                case 1:
                    tvOrderType.setText("大型");
                    break;
                case 2:
                    tvOrderType.setText("中型及以下");
                    break;
            }
            //联系人必填
            tvMeetingConstacts.setText(meetingBean.getData().getU_name());
            //号码必填项
            tvMeetingConstactsPhone.setText(meetingBean.getData().getPhonenum());
            //人数
            tvMeetingPersoncount.setText(meetingBean.getData().getNumber() + "人");
            //领导
            tvMeetingLeader.setText(meetingBean.getData().getLeader() == null ? "无" : meetingBean.getData().getLeader());
            //部门
            tvMeetingDepartment.setText(meetingBean.getData().getDepartmentDTO().getName());
            //会议开始时间
            tvMeetingStarttime.setText(meetingBean.getData().getStarttime());
            //会议日期
            tvMeetingDate.setText(meetingBean.getData().getSelectdate());
            //坐标会牌
            if (!TextUtils.isEmpty(meetingBean.getData().getSeat_card())) {
                liSitdownCard.setVisibility(View.VISIBLE);
                tvMeetingSit.setText(meetingBean.getData().getSeat_card());
            }
            if (!TextUtils.isEmpty(meetingBean.getData().getHuibiao_info())) {
                liMeetingFlag.setVisibility(View.VISIBLE);
                tvMeetingFlag.setText(meetingBean.getData().getHuibiao_info());
            }

        }
    }

    /***
     * 获取列表
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void findorderdMeeting(FindOrderdMeetingEvent eva) {
        if (eva.isOk()) {
            meetingBean = eva.getBean();
            initui();
            inittag();
        } else {
            ToastUtils.showToast(eva.getCodeDescribe());
        }
    }

    /***
     * 查找用户
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void finduser(FindPersonByUid eva) {
        if (eva.isOk()) {
           PersonBean personBean = eva.getBean().getData();
            if (personBean != null) {
                userList.add(personBean);
            }
            configRecyclerview();

        }
    }

    private void configRecyclerview() {
        constactsAdpter.setUserList(userList);
        constactsAdpter.notifyDataSetChanged();
    }


}
